﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：zhaijc
 * 创建时间：2023/4/14 11:32:19
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MedianSocketService.utils
{
    public class MyByteUtil
    {
        #region __字节型转十六进制字符串__
        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2") + " ";
                }
            }
            return returnStr;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hexString">字符串 "AC FD FD AC" </param>
        /// <returns></returns>
        public static byte[] StrToToHexByte(string hexString)
        {
            string[] hexValuesSplit = hexString.Split(' ');
            byte[] bytes = new byte[hexValuesSplit.Length];
            for (int i = 0; i < hexValuesSplit.Length; i++)
            {
                bytes[i] = Convert.ToByte(hexValuesSplit[i], 16);
            }
            return bytes;

        }
        #endregion


        /// <summary>
        /// 整型转字节,低字节在前-高字节在后
        /// </summary>
        /// <param name="caseNo"></param>
        /// <returns></returns>
        public static List<byte> IntToByte2(int data)
        {
            List<byte> address = new List<byte>();
            var udataLength = (ushort)(data);
            address.Add((byte)(0xff & udataLength));
            address.Add((byte)((0xff00 & udataLength) >> 8));
            return address;
        }
        /// <summary>
        /// 整型转字节,低字节在前-高字节在后
        /// </summary>
        /// <param name="caseNo"></param>
        /// <returns></returns>
        public static List<byte> IntToByte4(int data)
        {
            List<byte> address = new List<byte>();
            address.Add((byte)(0xff & data));
            address.Add((byte)((0xff00 & data) >> 8));
            address.Add((byte)((0xff00 & data) >> 16));
            address.Add((byte)((0xff00 & data) >> 24));
            return address;
        }
        /// <summary>
        /// 整数转四个字节
        /// </summary>
        /// <param name="data"></param>
        /// <param name="low">true:低字节序，false:高字节序</param>
        /// <returns></returns>
        public static List<byte> DoubleToByte2(double data, bool low = true)
        {
            var intData = Math.Round(data, MidpointRounding.AwayFromZero);
            List<byte> address = new List<byte>();
            var udataLength = (int)(intData);
            if (low)
            {
                address.Add((byte)(0xff & udataLength));
                address.Add((byte)((0xff00 & udataLength) >> 8));
            }
            else
            {
                address.Add((byte)((0xff00 & udataLength) >> 8));
                address.Add((byte)(0xff & udataLength));
            }
            return address;
        }
        /// <summary>
        /// 整型转字节,低字节在前-高字节在后
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List<byte> DoubleToByte4(double data)
        {
            var intData = Math.Round(data, MidpointRounding.AwayFromZero);
            List<byte> address = new List<byte>();
            var udataLength = (int)(intData);
            address.Add((byte)(0xff & udataLength));
            address.Add((byte)((0xff00 & udataLength) >> 8));
            address.Add((byte)((0xff0000 & udataLength) >> 16));
            address.Add((byte)((0xff000000 & udataLength) >> 24));
            return address;
        }
        /// <summary>
        /// CRC16
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static List<byte> Crc16(byte[] buffer)
        {
            List<byte> byteDate = new List<byte>();
            ushort crc = 0xFFFF;
            int size = buffer.Length;
            int i = 0;
            if (size > 0)
            {
                while (size-- > 0)
                {
                    crc = (ushort)((crc >> 8) | (crc << 8));
                    crc ^= buffer[i++];
                    crc ^= (ushort)(((byte)crc) >> 4);
                    crc ^= (ushort)(crc << 12);
                    crc ^= (ushort)((crc & 0xff) << 5);
                }
            }
            byteDate.Add((byte)((crc >> 8) & 0xff));
            byteDate.Add((byte)(crc & 0xff));
            return byteDate;
        }
        /// <summary>
        /// 求CRC校验码
        /// </summary>
        /// <param name="byteDate"></param>
        /// <returns></returns>
        public static byte GetCrc(byte[] byteDate)
        {
            for (int i = 0; i < byteDate.Length - 1; i++)
            {
                var r = (byte)(byteDate[i] ^ byteDate[i + 1]);
                byteDate[i + 1] = r;
            }
            return byteDate[byteDate.Length - 1];
        }
       

    }
}
